package com.qf.flash.ui.chat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.event.ChangeGroupEvent;
import com.qf.flash.model.GroupDetailBean;
import com.qf.flash.mvp.contract.GroupSetContract;
import com.qf.flash.mvp.presenter.GroupDetailPresenter;
import com.qf.flash.ui.chat.adapter.TeamChatItem;
import com.qf.flash.ui.home.BusinessDetailActivity;
import com.qf.flash.utils.ImageLoader;
import com.qf.flash.widget.RatingBarView;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 聊天群组设置activity
 */
public class GroupSetsActivity extends BaseActivity implements GroupSetContract.View {

    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.iv_team_detail)
    ImageView ivTeamDetail;
    @BindView(R.id.iv_top_head)
    ImageView iv_top_head;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_group_name)
    TextView tvGroupName;
    @BindView(R.id.iv_shop_head)
    ImageView ivShopHead;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.shop_star)
    RatingBarView shopStar;
    @BindView(R.id.tv_shop_fen)
    TextView tvShopFen;
    @BindView(R.id.iv_shop_detail)
    ImageView ivShopDetail;
    @BindView(R.id.tv_logout_group)
    TextView tvLogoutGroup;
    @BindView(R.id.tv_bus_empty)
    TextView tv_bus_empty;
    @BindView(R.id.tv_shop)
    TextView tv_shop;
    @BindView(R.id.ll_shop_msg)
    LinearLayout ll_shop_msg;
    @BindView(R.id.cv_option)
    CardView cv_option;
    private GroupDetailPresenter groupDetailPresenter;
    private String groupId;
    private RVAdapter<GroupDetailBean.UsersBean> mAdapter;
    private GroupDetailBean groupBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_chat_group_sets;
    }

    @Override
    protected void initInject() {
        super.initInject();
        groupDetailPresenter = new GroupDetailPresenter();
        initPresenters(groupDetailPresenter);
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("群组设置");
        groupId = getIntent().getStringExtra("groupId");
        groupDetailPresenter.getGroupDetails(groupId);
        mAdapter = new RVAdapter<>(new TeamChatItem());

        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 5));
        mRecyclerView.setAdapter(mAdapter);


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_top_back, R.id.iv_team_detail, R.id.iv_shop_detail, R.id.ll_team_detail, R.id.tv_logout_group})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.iv_team_detail:
            case R.id.ll_team_detail:
                ChatGroupDetailsActivity.start(this, groupBean);
                break;
            case R.id.iv_shop_detail:
                if (groupBean != null && !TextUtils.isEmpty(groupBean.getShopId()))
                    BusinessDetailActivity.start(this, groupBean.getOrderId(), groupBean.getShopId(), "", false);
                break;
            case R.id.tv_logout_group: //  退群or解散
                groupDetailPresenter.outGroup(groupId);
                break;
        }
    }

    public static void start(Activity activity, String groupId) {
        Intent intent = new Intent(activity, GroupSetsActivity.class);
        intent.putExtra("groupId", groupId);
        activity.startActivity(intent);
    }


    @Override
    public void setUi(GroupDetailBean groupDetailBean) {
        groupBean = groupDetailBean;
        tvTitle.setText(groupDetailBean.getTeamName());
        tvGroupName.setText("队名：" + groupDetailBean.getTeamName());
        tvTime.setText("时间：" + groupDetailBean.getGameTime());
        List<GroupDetailBean.UsersBean> users = groupDetailBean.getUsers();
        mAdapter.setDatas(users);
        if (groupBean.getCaptainId().equals(UserMgr.getInstance().getLoginUser().getUid())) {
            tvLogoutGroup.setText("解散组队");
        }
        for (GroupDetailBean.UsersBean user : users) {
            if (TextUtils.equals(user.getId(), UserMgr.getInstance().getLoginUser().getUid())) {
                cv_option.setVisibility(View.VISIBLE);
            }
        }
        ImageLoader.loadRoundCorners(this, groupDetailBean.getPlayImage(), iv_top_head, 5);
        mAdapter.notifyDataSetChanged();

        setShopper(groupDetailBean.getShopId(), groupDetailBean);
    }

    @Override
    public void groupOptionSuccess() {
        finish();
        EventBus.getDefault().post(new ChangeGroupEvent());
    }

    /**
     * 设置商家信息
     */
    private void setShopper(String shopID, GroupDetailBean groupDetailBean) {
        if (!TextUtils.isEmpty(shopID)) {
            ll_shop_msg.setVisibility(View.VISIBLE);
            tv_bus_empty.setVisibility(View.GONE);
            tvShopName.setText(groupDetailBean.getShopName());
            tvShopFen.setText(groupDetailBean.getShopScore() + "分");
            try {
                shopStar.setStar((int) Math.round(Double.parseDouble(groupDetailBean.getShopScore())), false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            tv_shop.setText("商家: " + groupDetailBean.getShopName());
            ImageLoader.loadCircleHead(this, groupDetailBean.getShopPhoto(), ivShopHead);

        } else {
            ll_shop_msg.setVisibility(View.GONE);
            ivShopDetail.setVisibility(View.GONE);
            tv_bus_empty.setVisibility(View.VISIBLE);
        }
    }


    @Override
    protected boolean useEvent() {
        return true;
    }

    @Subscribe
    public void onChangeGroupEvent(ChangeGroupEvent event) {
        groupDetailPresenter.getGroupDetails(groupId);
    }
}
